package com.yoyosys.echo.manager.cluster.service.impl;

import com.alibaba.fastjson.JSON;
import com.yoyosys.echo.manager.system.common.Message;
import com.yoyosys.echo.manager.cluster.dao.TaskInstanceMapper;
import com.yoyosys.echo.manager.cluster.po.TaskInstance;
import com.yoyosys.echo.manager.cluster.service.TaskInstanceService;
import com.yoyosys.echo.manager.cluster.vo.TaskInstanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TaskInstanceServiceImpl implements TaskInstanceService {
    @Autowired
    private TaskInstanceMapper taskInstanceMapper;
    @Override
    public Message joblog(TaskInstanceVo taskInstance) {
            Message message = new Message();
            try {
                TaskInstance taskInstances = new TaskInstance();
                taskInstances.setId(taskInstance.getId());
                taskInstances.setUnitProcId(taskInstance.getUnitProcId());
                taskInstances.setUnitCode(taskInstance.getUnitCode());
                taskInstances.setBatchNo(taskInstance.getBatchNo());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String data = sdf.format(new Date());
                taskInstances.setCreateDate(data);
                taskInstances.setStartDate(data);
                taskInstances.setLastUpdateDate(data);
                taskInstances.setMessage(taskInstance.getMessage());
                taskInstances.setState(taskInstance.getState());
                taskInstances.setRequestId(taskInstance.getRequestId());
                if (taskInstance.getDependencys() != null){
                    List<TaskInstance> execLogs = taskInstance.getDependencys().stream()
                            .map(dto -> {
                                TaskInstance task = new TaskInstance();
                                task.setCreateDate(taskInstances.getCreateDate());
                                task.setLastUpdateDate(taskInstances.getLastUpdateDate());
                                task.setState(dto.getState());
                                task.setUnitCode(dto.getUnitCode());
                                task.setUnitProcId(taskInstances.getUnitProcId());
                                task.setMessage(dto.getMessage());
                                return task;
                            }).collect(Collectors.toList());
                    taskInstances.setDependencys(JSON.toJSONString(execLogs));
                }
                taskInstanceMapper.saveTaskInstance(taskInstances);
                message.setSuccessMsg("任务实例提交成功");
                message.setData(taskInstances.getRequestId());
                message.setCode("200");
            }catch (Exception e){
                message.setErrorMsg("任务实例提交失败");
                message.setCode("500");
                message.setData(e.getMessage());
            }
            return message;
    }
}
